﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QRandomGenerator>
#include <QThread>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

//    frame = new MyPlotFrame();
//    frame->InitCurve();
//    frame->HideAllCurve();
//    frame->SetVisible(CurveTypeEnum::ForceSumC);
//    frame->SetVisible(CurveTypeEnum::PowerC);
//    ui->horizontalLayout->addWidget(frame);

//    demo = new MyPlotFrameDemo();
//    ui->horizontalLayout->addWidget(demo);

    demo1 = new QCustomPlotDemo1();
    ui->horizontalLayout->addWidget(demo1);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{

//    QList<SamplingDataItem> curveData;
//    for(int i=0; i<200; ++i)
//    {
//        SamplingDataItem dataItem;
//        dataItem.Speed = QRandomGenerator::global()->bounded(-20,170);
//        dataItem.AddSpeed = QRandomGenerator::global()->bounded(2,7);
//        dataItem.BeforeForceL = QRandomGenerator::global()->bounded(-20,170);
//        dataItem.BeforeForceR = QRandomGenerator::global()->bounded(-20,170);
//        dataItem.AfterForceL = QRandomGenerator::global()->bounded(-20,170);
//        dataItem.AfterForceR = QRandomGenerator::global()->bounded(-20,170);
//        dataItem.ForceSum = QRandomGenerator::global()->bounded(-20,170);
//        dataItem.Power = QRandomGenerator::global()->bounded(-20,170);
//        dataItem.Mileage = QRandomGenerator::global()->bounded(-20,170);
//        dataItem.BkPoint = i;
//        dataItem.CtrlType = QRandomGenerator::global()->bounded(-20,170);
//        dataItem.OutDesV = QRandomGenerator::global()->bounded(-20,170);
//        dataItem.CtrlDes = QRandomGenerator::global()->bounded(-20,170);
//        dataItem.SampPointNo = i;
//        dataItem.Timestamp = i;
//        curveData.append(dataItem);
//        if(i%10==0)
//        {
//            QThread::msleep(40);
//            frame->DrawLine(curveData);
//            curveData.clear();
//        }
//    }

//        demo->ToggleGraph0();
//        demo->ToggleGraph1();

        QList<SamplingDataItem> curveData;
        int j = 0;
        for(int i=0; i<19500; ++i)
        {
            SamplingDataItem dataItem;
            if(i/1000==0)
                dataItem.Speed = QRandomGenerator::global()->bounded(-1,1);
            else if(i/1000>0 && i/1400==0)
                dataItem.Speed = (i-1400)/((1000-1400)/(18-(-1)));
            else if(i/1400>0 && i/2200==0)
                dataItem.Speed = (2200-i)/((2200-1400)/(18-11));
            else if(i/2200>0 && i/2800==0)
                dataItem.Speed = (2800-i)/((2800-2200)/(14-0));
            else if(i/2800>0 && i/4800==0)
                dataItem.Speed = (4800-i)/((4800-2800)/(1-(-1)));
            else if(i/4800>0 && i/5400==0)
                dataItem.Speed = -(5400-i)/((5400-4800)/((14)-(0)));
            else if(i/5400>0 && i/5600==0)
                dataItem.Speed = (5600-i)/((5600-5400)/((14)-(12)));
            else if(i/5600>0 && i/6000==0)
                dataItem.Speed = (6000-i)/((6000-5600)/((30)-(12)));
            else if(i/6000>0 && i/8500==0)
                dataItem.Speed = (8500-i)/((8500-6000)/((32)-(28)));
            else if(i/8500>0 && i/9500==0)
                dataItem.Speed = (9500-i)/((9500-8500)/((32)-(0)));
            else if(i/9500>0 && i/11700==0)
                dataItem.Speed = (11700-i)/((11700-9500)/((1)-(-1)));
            else if(i/11700>0 && i/12200==0)
                dataItem.Speed = (12200-i)/((12200-11700)/((10)-(-1)));
            else if(i/12200>0 && i/12400==0)
                dataItem.Speed = (12400-i)/((12400-12200)/((14)-(11)));
            else if(i/12400>0 && i/13200==0)
                dataItem.Speed = (13200-i)/((13200-12400)/((32)-(14)));
            else if(i/13200>0 && i/13400==0)
                dataItem.Speed = (13400-i)/((13400-13200)/((36)-(32)));
            else if(i/13400>0 && i/14200==0)
                dataItem.Speed = (14200-i)/((14200-13400)/((48)-(32)));
            else if(i/14200>0 && i/15500==0)
                dataItem.Speed = (15500-i)/((15500-14200)/((48)-(46)));
            else if(i/15500>0 && i/16200==0)
                dataItem.Speed = (16200-i)/((16200-15500)/((36)-(32)));
            else if(i/16200>0 && i/17500==0)
                dataItem.Speed = (17500-i)/((17500-16200)/((36)-(32)));
            else if(i/17500>0 && i/18700==0)
                dataItem.Speed = (18700-i)/((18700-17500)/((34)-(0)));
            else if(i/18700>0 && i/19500==0)
                dataItem.Speed = (19500-i)/((19500-18700)/((2)-(-2)));
            else
                dataItem.Speed = QRandomGenerator::global()->bounded(-1,0);
            dataItem.AddSpeed = QRandomGenerator::global()->bounded(2,7);
            dataItem.BeforeForceL = QRandomGenerator::global()->bounded(-20,170);
            dataItem.BeforeForceR = QRandomGenerator::global()->bounded(-20,170);
            dataItem.AfterForceL = QRandomGenerator::global()->bounded(-20,170);
            dataItem.AfterForceR = QRandomGenerator::global()->bounded(-20,170);
            dataItem.ForceSum = QRandomGenerator::global()->bounded(-20,170);
            dataItem.Power = QRandomGenerator::global()->bounded(-20,170);
            dataItem.Mileage = QRandomGenerator::global()->bounded(-20,170);
            dataItem.BkPoint = i;
            dataItem.CtrlType = QRandomGenerator::global()->bounded(-20,170);
            dataItem.OutDesV = QRandomGenerator::global()->bounded(-20,170);
            dataItem.CtrlDes = QRandomGenerator::global()->bounded(-20,170);
            dataItem.SampPointNo = i;
            dataItem.Timestamp = i;
            curveData.append(dataItem);
            if(i%10==0)
            {
                QThread::msleep(10);
                demo1->DrawLine(curveData,curveData.length());
                curveData.clear();
            }
        }
}

